package com.rm.questions.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.questions.entity.CoreQLog;
import com.rm.questions.entity.CoreVersion;
import com.rm.questions.mapper.CoreQLogMapper;
import com.rm.questions.mapper.CoreQuestionMapper;
import com.rm.questions.mapper.CoreVersionMapper;
import com.rm.questions.service.ICoreVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 题库版本Service业务层处理
 *
 * @author sh
 * @date 2025-03-26
 */
@Service
public class CoreVersionServiceImpl extends ServiceImpl<CoreVersionMapper, CoreVersion> implements ICoreVersionService
{
    @Autowired
    private CoreVersionMapper coreVersionMapper;

    @Autowired
    private CoreQLogMapper coreQLogMapper;

    @Autowired
    private CoreQuestionMapper coreQuestionMapper;

    /**
     * 查询题库版本
     *
     * @param id 题库版本主键
     * @return 题库版本
     */
    public CoreVersion selectById(String id){
        return coreVersionMapper.selectById(id);
    }

    /**
     * 查询题库版本列表
     *
     * @param coreVersion 题库版本
     * @return 题库版本集合
     */
    public List<CoreVersion> selectList(CoreVersion coreVersion){
        return coreVersionMapper.selectList(coreVersion);
    }

    /**
     * 新增题库版本
     *
     * @param coreVersion 题库版本
     * @return 结果
     */
    public int insert(CoreVersion coreVersion){
        coreVersion.setCode(generateVersionCode());
        return coreVersionMapper.insert(coreVersion);
    }

    /**
     * 修改题库版本
     *
     * @param coreVersion 题库版本
     * @return 结果
     */
    public int update(CoreVersion coreVersion){
        return coreVersionMapper.update(coreVersion);
    }

    /**
     * 删除题库版本
     *
     * @param id 题库版本主键
     * @return 结果
     */
    public int deleteById(String id){
        return coreVersionMapper.deleteById(id);
    }

    /**
     * 批量删除题库版本
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteByIds(String ids){
        return coreVersionMapper.deleteByIds(Convert.toStrArray(ids));
    }

    /**
     * 跳转到指定版本
     *
     * @param versionId 题库版本
     * @return 结果
     */
    public int jumpToVersion(String versionId){
        CoreVersion toVersion = coreVersionMapper.selectById(versionId);
        CoreVersion fromVersion = coreVersionMapper.selectNewestVersion();
        List<CoreQLog> logs = coreQLogMapper.selectByTwoTime(toVersion.getCreateTime(),fromVersion.getCreateTime());
        for(CoreQLog log : logs) {
            if(log.getLogType().equals("insert")){
                CoreQLog coreQLog = new CoreQLog();
                coreQLog.setLogType("delete");
                coreQLog.setPreQuestion(log.getPostQuestion());
                coreQuestionMapper.deleteById(log.getQuestionId());
                coreQLogMapper.insert(coreQLog);
            }
            if(log.getLogType().equals("delete")){
                CoreQLog coreQLog = new CoreQLog();
                coreQLog.setLogType("insert");
                coreQLog.setPostQuestion(log.getPreQuestion());
                coreQuestionMapper.insert(log.getPreQuestion());
                coreQLogMapper.insert(coreQLog);
            }
            if(log.getLogType().equals("update")){
                CoreQLog coreQLog = new CoreQLog();
                coreQLog.setLogType("update");
                coreQLog.setPostQuestion(log.getPreQuestion());
                coreQLog.setPreQuestion(log.getPostQuestion());
                coreQuestionMapper.updateById(log.getPreQuestion());
                coreQLogMapper.insert(coreQLog);
            }
        }
        CoreVersion newVersion = new CoreVersion();
        newVersion.setName(toVersion.getName()+"(已跳转)");
        newVersion.setCode(generateVersionCode());
        coreVersionMapper.insert(newVersion);
        return 0;
    }

    /**
     * 生成版本号
     *
     * @return 版本号
     */
    private String generateVersionCode(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd.HH:mm:ss");
        String datePart = LocalDateTime.now().format(formatter);
        int randomNumber = ThreadLocalRandom.current().nextInt(10000);
        String randomPart = String.format("%04d", randomNumber);
        return "T"+datePart+"X"+randomPart;
    }

}